﻿Public Class FormUsuarioListado

    
    Private Sub FormUsuarioListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgUsuarios.AutoGenerateColumns = False

        CargarCombos()
        CargarUsuarios()
    End Sub

    Dim classController As New Class_RC_Controller.rc_Mantenimiento()

    Private m_frmUsuario As Frmusuario
    Public Property frmUsuarios() As Frmusuario
        Get
            Return m_frmUsuario
        End Get
        Set(value As Frmusuario)
            m_frmUsuario = value
        End Set
    End Property


    Private Sub CargarUsuarios()

        Dim codArea As Integer = 0
        If cbxArea.Checked Then
            codArea = cboArea.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Usuario_All(oiml.dbConn, 0, txtUsuario.Text, txtNombre.Text, txtApellido.Text, codArea)
        dgUsuarios.DataSource = dt

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCombos()
        Dim dsArea As DataTable
        dsArea = classController.get_Area_All(oiml.dbConn, 0, "")
        With Me.cboArea
            .DataSource = dsArea
            .DisplayMember = "descArea"
            .ValueMember = "codArea"
        End With
        cboArea.SelectedIndex = 0
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgUsuarios.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgUsuarios.SelectedRows(0).Cells(0).Value

                If frmUsuarios Is Nothing Then

                    frmUsuarios = New Frmusuario()
                    frmUsuarios.codUsuario = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmUsuarios.ShowDialog()
                    CargarUsuarios()
                    frmUsuarios.Dispose()
                    frmUsuarios = Nothing
                    'End If
                Else
                    frmUsuarios.codUsuario = cod
                    frmUsuarios.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarUsuarios()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown, txtApellido.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

    Private Sub dgUsuarios_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgUsuarios.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgUsuarios_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgUsuarios.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub txttxtUsuario_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtUsuario.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

    Private Sub txtDocumento_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtDocumento.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

    Private Sub cbxArea_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxArea.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

    Private Sub cboArea_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboArea.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarUsuarios()

        End If
    End Sub

End Class